package com.hatic.money;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;

/**
 * 投资计划调整后的处理。
 *
 * @author TonyTan
 * @version 1.0, 2011-10-18
 */
public class PlanChgBO extends BusinessObject {
	private static final long serialVersionUID = 1L;

	public String preAudit(String[] planIds) {
		if (planIds == null || planIds.length == 0) {
			setMessage("没有选择投资计划调整记录！");
			return _returnFaild;
		}
		
		for (int i = 0, n = planIds.length; i < n; i++) {
			Map<String,String> chgdata = queryChg(planIds[i]);
			String planId = chgdata.get("plan_id");
			String planMoney = chgdata.get("after_money");
			String yearMoney = chgdata.get("after_yearmoney");
			String changeType = chgdata.get("change_type");
			
			if (changeType.equals("1")) {
				updateStatus(planId);
			} else {
				updatePlan(planId, planMoney, yearMoney);
			}
			
			//更新父计划中的金额信息
			String parentId = getParentId(planId);
			if (parentId != null && parentId.length() > 0) {
				(new PlanCompBO()).sumMoney(parentId);
			}
		}
		
		return _returnSuccess;
	}
	
	//更新计划状态为中止
	private boolean updateStatus(String planId) {
		String sql = "update money_plan set plan_status = '92' where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		return _dao.update(param);
	}
	
	//更新投资计划的调概金额
	private boolean updatePlan(String planId, String planMoney, String yearMoney) {
		String sql = "update money_plan set plan_money = ?, check_moneychk = ?, year_money = ? where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addDoubleValue(planMoney);
		param.addDoubleValue(planMoney);
		param.addDoubleValue(yearMoney);
		param.addStringValue(planId);
		
		return _dao.update(param);
	}
	
	//取调整记录信息
	private Map<String,String> queryChg(String chgId) {
		String sql = "select after_money, after_yearmoney, change_type, plan_id from money_change where change_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(chgId);
		
		return _dao.queryMap(param);
	}
	
	//取投资计划父ID
	private String getParentId(String planId) {
		String sql = "select plan_parentid from money_plan where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		Map<String, String> mpData = _dao.queryMap(param);
		return mpData.get("plan_parentid");
	}
}
